package com.yf.stack.dynamiclinking;

import org.junit.Test;

public class AnimalTest {
    public void eat(Animal animal) {
        animal.eat(); //晚期绑定,late binding, 动态绑定
    }

    public void hunt(Huntable huntable) {
        huntable.hunt(); //晚期绑定,late binding，动态绑定
    }

    @Test
    public void testCat() {
        Cat cat = new Cat();
        eat(cat);
        hunt(cat);
    }

    @Test
    public void testDog() {
        Dog dog = new Dog();
        eat(dog);
        hunt(dog);
    }

}

interface Huntable {
    public void hunt();
}

class Animal {
    public void eat() {
        System.out.println("动物都是吃货");
    }
}

class Cat extends Animal implements Huntable {
    private String name;

    public Cat() {
        super(); // early binding, 早期绑定，静态绑定
    }

    public Cat(String name) {
        this(); // early binding, 早期绑定，静态绑定
        this.name = name;
    }

    @Override
    public void hunt() {
        System.out.println("猫捉老鼠，天经地义");
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

class Dog extends Animal implements Huntable {
    @Override
    public void hunt() {
        System.out.println("狗拿耗子，多管闲事");
    }

    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }
}

